package com.nuatar.meibi;

import java.util.HashMap;
import java.util.Map;

import com.nuatar.meibi.pubvar.PhoneMaster;
import com.nuatar.meibi.pubvar.PostMan;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SugActivity extends FatherActivity {
	String imei;
	Map<String, String> params;
	static Handler mhandler;
	String ancs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sug);
		CrashHandler crashHandler = CrashHandler.getInstance();  
        crashHandler.init(this); 
		mhandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
					Toast.makeText(getApplicationContext(), "建议发送成功，谢谢!",
							Toast.LENGTH_SHORT).show();
					finish();
				} else if (msg.what == 2) {
					Toast.makeText(getApplicationContext(), "发送失败，请重试"+ancs, Toast.LENGTH_SHORT).show();
				}
			}
		};
		final EditText et = (EditText) findViewById(R.id.SugText);
		TelephonyManager telephonyManager = (TelephonyManager) this
				.getSystemService(Context.TELEPHONY_SERVICE);
		imei = PhoneMaster.getMD5Str(telephonyManager.getDeviceId());
		Button bt = (Button) findViewById(R.id.SugButton);
		bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String info = et.getText().toString();
				if(info.equals("")){
					info="催更";
				}
				params = new HashMap<String, String>();
				params.put("info", info);
				params.put("did", imei);
				MT mt = new MT();
				new Thread(mt).start();
			}
		});
	}

	class MT implements Runnable {

		@Override
		public void run() {
			String anc = PostMan.submitPostData(params, "utf-8","action=PostSug"
					);
			Message msg = new Message();
			if (anc.equals("OK")) {
				msg.what = 1;
			} else {
				msg.what = 2;
				ancs=anc;
			}
			SugActivity.mhandler.sendMessage(msg);
		}
	}
	@Override
	protected void onStart() {
		super.onStart();
		mGaTracker.sendView(this.toString());
	}

}
